<template>
	<view>
		
		<view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<button class=" bg-white  headimg" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
						<view class="cu-avatar lg round" :style="'background-image:url('+client.headimg+');'"></view>
					</button>
					<input class="text-right" type="nickname" v-model="client.nc" placeholder="请输入昵称" />
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<view class="title">电话</view>
					<input class="text-right" v-model="client.tel" placeholder="请输入电话" />
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<view class="title">人脸</view>
					<view @click="openTx" class="text-right flex-sub">
						{{client.tx ? "已设置" : "未设置"}}
					</view>
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view v-if="client.tx" class="bg-white">
				<image class="w-100" :src="client.tx" mode="aspectFill"></image>
			</view>
		</view>
		<view class="padding ">
			<button @click="save" class="cu-btn block bg-main lg">保存</button>
		</view>
	</view>
</template>

<script>
	import upload from "@/wx-cos/upload.js";
	export default {
		data: function() {
			return {
				txurl: "",
				client: this.store.state.client,
			};
		},
		onShareAppMessage: function(){
			return {
				title: this.store.state.title,
				path: '/pages/first?id='+this.store.state.client.clientid
			}
		},
		onLoad: function() {
			this.api("/client/info/get").send({
				clientid: this.client.clientid,
			}).then(res => {
				this.client = res.client;
			});
		},
		methods: {
			onChooseAvatar: function(e) {
				// 上传图片
				// 得到图片云服务地址
				upload.checkUploadFile(
					e.detail.avatarUrl,
					this.client.clientid,
					"headimg", pro => {}
				).then(data => {
					this.client.headimg = data.url;
					this.$forceUpdate();
				});
			},
			openTx: function() {
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					sizeType: ['original'],
					camera: "back",
					success: res => {
						upload.checkUploadFile(
							res.tempFiles[0].tempFilePath,
							this.client.clientid,
							"face", progressData => {
								console.log(progressData);
							}).then((data) => {
							this.client.tx = data.url;
						});
					}
				})
			},
			save: function() {
				this.api("/client/info/put").send({
					client: this.client
				}).then(res => {
					this.message.info("修改成功");
					uni.navigateBack();
				});
			}
		}
	}
</script>

<style scoped>
	.headimg {
		padding-left: 0;
		padding-right: 0;
	}

	.headimg::after {
		border: none;
	}
</style>


<template>
	<view>
		<bg-video></bg-video>
		<cu-custom bgColor="bg-main" :isBack="true">
			<block class="text-white" slot="content">设置</block>
		</cu-custom>
		<view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<button class=" bg-white  headimg" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
						<view class="cu-avatar lg round" :style="'background-image:url('+client.headimg+');'"></view>
					</button>
					<input class="text-right" type="nickname" v-model="client.nc" placeholder="请输入昵称" />
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<view class="title">电话</view>
					<text>
						<text class="cuIcon-phone text-main"></text>
						<!-- <input class="text-df" v-model="client.tel" placeholder="请输入电话" /> -->
						<text v-if="client && client.tel" class="text-sm phone-button">{{client.tel}}</text>
						<button v-else class="phone-button text-sm" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">请输入手机号</button>
					</text>
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
			<view class="solid-bottom">
				<view class="cu-form-group">
					<view class="title">人脸</view>
					<view @click="openTx" class="relative cu-avatar xl bg-inherit">
						<view v-if="client && client.tx" class="cu-avatar xl round absolute top0 right0"
							:style="'background-image:url('+client.tx+');'"></view>
						<view v-if="client && client.tx"
							class="cu-avatar xl round absolute top0 right0 bg-cover"></view>
					</view>
					<text class="cuIcon-right text-gray"></text>
				</view>
			</view>
		</view>
		<view class="padding ">
			<button @click="save" class="cu-btn block bg-main lg">保存</button>
		</view>
	</view>
</template>

<script>
	import upload from "@/wx-cos/upload.js";
	export default {
		data: function() {
			return {
				txurl: "",
				client: this.store.state.client,
				// client: {},
			};
		},
		onShareAppMessage: function() {
			return {
				title: this.store.state.title,
				path: '/pages/first?id=' + this.store.state.client.clientid
			}
		},
		created: function() {
		},
		mounted:function() {
		},
		methods: {
			initClient: function() {
				this.api("/client/info/get").send({
					clientid: this.store.state.client.clientid,
				}).then(res => {
					this.client = res.client;
				});
			},
			getPhoneNumber: function(e) {
				let phoneCode = e.detail.code;
				
				if (phoneCode) {
					this.api("/dz/phone").send({
						clientid: this.client.clientid,
						dzid: this.store.state.dz.dzid,
						phoneCode,
					}).then(data => {
						this.store.commit("submitClient", data.client);
						this.store.commit("submitDz", data.adz);
						this.client = data.client;
						this.message.info("电话设置成功");
					});
				}

			},
			onChooseAvatar: function(e) {
				// 上传图片
				// 得到图片云服务地址
				upload.checkUploadFile(
					e.detail.avatarUrl,
					this.client.clientid,
					"headimg", pro => {}
				).then(data => {
					this.client.headimg = data.url;
					this.save();
				});
			},
			openTx: function() {
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					sizeType: ['original'],
					camera: "back",
					success: res => {
						upload.checkUploadFile(
							res.tempFiles[0].tempFilePath,
							this.client.clientid,
							"face", progressData => {
								console.log(progressData);
							}).then((data) => {
							this.client.tx = data.url;
							this.save();
						});
					}
				})
			},
			save: function() {
				this.showFlg = false;
				this.$forceUpdate();
				this.api("/dz/info/put").send({
					client: this.client
				}).then(res => {
					this.showFlg = true;
					this.client = res.client;
					this.adz = res.adz;
					this.store.commit("submitClient", res.client);
					this.store.commit("submitDz", res.adz);
					this.message.info("修改成功");
					
				});
				
				
			}
		}
	}
</script>

<style scoped>
	.headimg {
		padding-left: 0;
		padding-right: 0;
	}

	.headimg::after {
		border: none;
	}
	
	.phone-button {
		border-style: none;
		background-color: rgba(0, 0, 0, 0);
		padding: 0 0 0 5px;
		margin: 0;
	}
	
	.phone-button::after {
		border-style: none;
		background-color: rgba(0, 0, 0, 0);
	}
</style>